const favoriteModel = require('../models/favoriteModel');
const { sendSuccess, sendError } = require('../utils/response');

module.exports = {
  // 添加收藏
  async addFavorite(req, res) {
    try {
      const { id: userId } = req.user;
      const { product_id } = req.body;
      
      if (!product_id) {
        return sendError(res, '缺少商品ID', 400);
      }
      
      await favoriteModel.addFavorite(userId, product_id);
      sendSuccess(res, null, '商品已添加到收藏夹');
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 移除收藏
  async removeFavorite(req, res) {
    try {
      const { id: userId } = req.user;
      const { product_id } = req.params;
      
      await favoriteModel.removeFavorite(userId, product_id);
      sendSuccess(res, null, '商品已从收藏夹移除');
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 获取收藏列表
  async getFavorites(req, res) {
    try {
      const { id: userId } = req.user;
      const { page = 1, limit = 10 } = req.query;
      
      const { favorites, pagination } = await favoriteModel.getUserFavorites(userId, page, limit);
      sendSuccess(res, { favorites, pagination });
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 检查是否已收藏
  async checkFavorite(req, res) {
    try {
      const { id: userId } = req.user;
      const { product_id } = req.params;
      
      const isFavorite = await favoriteModel.checkFavorite(userId, product_id);
      sendSuccess(res, { is_favorite: isFavorite });
    } catch (err) {
      sendError(res, err.message);
    }
  }
};